package com.blog.controller;

import com.blog.common.CommonRespDto;
import com.blog.common.Constant;
import com.blog.common.RespBean;
import com.blog.config.JwtProperties;
import com.blog.seckill.orders.GoodRequest;
import com.blog.seckill.orders.SecOrders;
import com.blog.service.SecOrdersBaseService;
import com.blog.utils.JwtVerifyUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @ClassName SecOrdersBaseController
 * @Destription SecOrders基础：增删改查
 * @Author 天堂小野花
 * @Date 2025/8/19  19:16
 * @Version 1.0
 */
@RestController
@RequestMapping("/orders")
public class SecOrdersBaseController {
    @Autowired
    SecOrdersBaseService secOrdersBaseService;
    @Autowired
    JwtProperties jwtProperties;
    @PutMapping("/createOrder")
    public CommonRespDto createOrder(@RequestBody GoodRequest goodRequest,
                                     HttpServletRequest request){
        Map<String, Object> jwtHr = new JwtVerifyUtil().parseJwt(request.getHeader(Constant.TOKEN),jwtProperties.getSecretKey(),jwtProperties.getValidateInMs());
        Integer userId  = (Integer) jwtHr.get("id");
        if(secOrdersBaseService.createOrder(userId,goodRequest.getGoodId(),goodRequest.getGoodNum()) == 1){
           return CommonRespDto.success(null,"创建成功");
        }
        return CommonRespDto.success(null,"创建失败");
    }
    @PutMapping("/createOrderAi")
    public CommonRespDto createOrderAi(
            @RequestParam("goodId") Integer goodId,
            @RequestParam("goodNum") Integer goodNum,
            @RequestParam("userId") Integer userId){
        if(secOrdersBaseService.createOrder(userId,goodId,goodNum) == 1){
            return CommonRespDto.success(null,"创建成功");
        }
        return CommonRespDto.success(null,"创建失败");
    }
    @GetMapping("/selectAllOrders")
    public CommonRespDto selectAllOrders(){
        return CommonRespDto.success(secOrdersBaseService.selectAllOrders());
    }
    @GetMapping("/base/selectSecOrdersById/{rid}")
    public CommonRespDto selectSecOrdersById(@PathVariable Integer rid){
        return CommonRespDto.success(secOrdersBaseService.selectSecOrdersById(rid));
    }

    @PostMapping("/base/insertSecOrders")
    public RespBean insertSecOrders(@RequestBody SecOrders secOrders){
        if(secOrdersBaseService.insertSecOrders(secOrders) == 1 ){
            return RespBean.ok("添加成功");
        }
        return RespBean.error("添加失败");
    }
    @DeleteMapping("/base/deleteSecOrders/{rid}")
    public RespBean deleteSecOrdersById(@PathVariable Integer rid){
        if(secOrdersBaseService.deleteSecOrdersById(rid) == 1){
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }
    @PutMapping("/base/updateSecOrders")
    public RespBean updateSecOrders(@RequestBody SecOrders secOrders){

        if(secOrdersBaseService.updateSecOrders(secOrders) == 1){
            return RespBean.ok("更新成功");
        }
        return RespBean.error("更新失败");
    }
}
